package drukarnia.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import drukarnia.controller.FTPManager;
import drukarnia.controller.OrderManager;
import drukarnia.model.Orders;
import drukarnia.model.User;

@WebServlet("/OrderAction")
public class OrderAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public OrderAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesja = request.getSession(true);
		User user = (User) sesja.getAttribute("user");
		Orders order = new Orders();
		OrderManager linkController = new OrderManager();
		
		String material = request.getParameter("material");
		try {
			BeanUtils.populate(order, request.getParameterMap());
			linkController.saveOrder(order, user.getId_user(), material, order.getFileName());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
//		String cost = "";
//		if(order.getCost() != 0.0){
//			cost = order.getCost() + " PLN";
//		}else{
//			cost="obliczany tylko dla plikow *.tiff";
//		}
//		
//		String x = cost;
//		response.sendRedirect("confirmDate.jsp?correct="+order.getId_order()+"&cost=" + x);
		response.sendRedirect("confirmDate.jsp?correct="+order.getId_order());
		   
		
	}

}
